package vo;

import constant.Common;
import io.swagger.annotations.ApiModelProperty;
import util.lambda.Empty;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 列表状态统计
 *
 * @author zhanglinfeng
 * @date 2023-05-17 11:59:26
 */
public class StatusCountVO {
    @ApiModelProperty(value = "当前状态数量")
    private Integer currentStatusCount;
    @ApiModelProperty(value = "总数量")
    private Integer totalCount;

    public StatusCountVO() {
    }

    public StatusCountVO(Map<String, Integer> countMap, String currentStatus, List<String> statusList) {
        this.totalCount = 0;
        Class<?> objClass = this.getClass();
        for (Field field : objClass.getDeclaredFields()) {
            String status = field.getName().replace(Common.COUNT, Common.BLANK).toUpperCase();
            if (statusList.contains(status)) {
                int count = Optional.ofNullable(countMap.get(status)).orElse(0);
                this.totalCount = this.totalCount + count;
                try {
                    field.set(this, count);
                } catch (IllegalAccessException ignored) {
                }
            }
        }
        this.currentStatusCount = Empty.of(currentStatus).map(t -> Optional.ofNullable(countMap.get(t)).orElse(0)).orElse(totalCount);
    }


    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }


    public Integer getCurrentStatusCount() {
        return currentStatusCount;
    }

    private void setCurrentStatusCount(Integer currentStatusCount) {
        this.currentStatusCount = currentStatusCount;
    }

    @Override
    public String toString() {
        return "StatusCountVO{" +
                "currentStatusCount=" + currentStatusCount +
                ", totalCount=" + totalCount +
                '}';
    }
}